<?php
/**
 * ModuleError.php
 * 
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 * @version    0.1
 */

/** base class */
require_once 'Zend/Controller/Plugin/Abstract.php';

/**
 * Naneau_Controller_Plugin_ModuleError
 * 
 * module based error handling
 *
 * @category   Naneau
 * @package    Naneau_Plugin
 * @copyright  Copyright (c) 2007 Maurice Fonk - http://naneau.nl
 */
class Naneau_Controller_Plugin_ModuleError extends Zend_Controller_Plugin_Abstract {
    
    /**
     * prior to every dispatched action, 
     * 
     * set the error handler to the current module if and only if said module has
     * an errorcontroller.
     *
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request) {
        $front = Zend_Controller_Front::getInstance();
        //front controller
        $errorHandler = $front->getPlugin('Zend_Controller_Plugin_ErrorHandler');
        //the error handler plugin
        $moduleName = $this->getRequest()->getModuleName();
        //module name for the current request

        $request = clone $this->getRequest();
        $request->setModuleName($moduleName);
        $request->setActionName($errorHandler->getErrorHandlerAction());
        $request->setControllerName($errorHandler->getErrorHandlerController());
        //clone the request and make it like an error handling request
        
        if ($front->getDispatcher()->isDispatchable($request)) {
            $errorHandler->setErrorHandlerModule($moduleName);
        }
        //modify the error handler module if the request would be valid
    }
}